package ch.zbw.pra.sysventory.domain.scanner;

import java.util.ArrayList;
import java.util.List;

import ch.zbw.pra.sysventory.domain.model.Printer;
import ch.zbw.pra.sysventory.domain.util.MyLogger;
import ch.zbw.pra.sysventory.ui.DetailLabel;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class ScanPrinter {

	private static final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(ScanPrinter.class);

	private String namePrinter;
	private String driverNamePrinter;
	private String driverVersionPrinter;

	private List<Printer> printers;

	// Constructor
	public ScanPrinter() {
		this.printers = new ArrayList<Printer>();

	}

	public List<Printer> getPrinter() {

		List<String> pPrinter = new ArrayList<String>();
		List<String> pDriver = new ArrayList<String>();

		String host = "localhost";
		String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);
		String query = "SELECT * FROM Win32_Printer";

		try {
			ActiveXComponent axWMI = new ActiveXComponent(connectStr);
			// Execute the query
			Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));

			// The Result is a Collection
			EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
			Dispatch item = null;
			while (enumVariant.hasMoreElements()) {
				item = enumVariant.nextElement().toDispatch();
				// Dispatch.call returns a Variant which we can convert to a
				// java
				// form.
				String namePrinter = Dispatch.call(item, "Name").toString();
				String DriverNamePrinter = Dispatch.call(item, "DriverName").toString();

				pPrinter.add(namePrinter);
				pPrinter.add(DriverNamePrinter);
			}

			query = "SELECT * FROM Win32_PrinterConfiguration";
			vCollection = axWMI.invoke("ExecQuery", new Variant(query));
			enumVariant = new EnumVariant(vCollection.toDispatch());
			item = null;
			while (enumVariant.hasMoreElements()) {
				item = enumVariant.nextElement().toDispatch();
				// Dispatch.call returns a Variant which we can convert to a
				// java
				// form.

				String DriverVersionPrinter = Dispatch.call(item, "DriverVersion").toString();
				String DriverCaptionPrinter = Dispatch.call(item, "Caption").toString();

				pDriver.add(DriverCaptionPrinter);
				pDriver.add(DriverVersionPrinter);
			}

			for (int i = 0; i < pPrinter.size(); i = i + 2) {
				namePrinter = pPrinter.get(i);
				driverNamePrinter = pPrinter.get(i + 1);
				driverVersionPrinter = pDriver.get(i + 1);

				MyLogger.logDebug(log, "Drucker: " + namePrinter + " Treiber: " + driverNamePrinter
						+ " Treiberversion: " + driverVersionPrinter);
				DetailLabel.setTextDetailLabel(namePrinter);
				Printer printer = new Printer(namePrinter, driverNamePrinter, driverVersionPrinter);

				printers.add(printer);
			}
		} catch (Exception e) {
			MyLogger.logError(log, e.toString());
		}

		return printers;

	}
}
